/*
 *  Copyright 2012, Tera-soft Co., Ltd.  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF TERA-SOFT CO.,
 *  LTD.  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIRD
 *  PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF TERA-SOFT CO., LTD
 *
 */
package com.tera.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tera.sys.dao.MenuDaoMapper;
import com.tera.sys.model.Menu;
import com.tera.sys.service.MenuService;
import com.tera.util.BeanUtils;

/**
 * @author Wallace chu
 *
 */
@Service
public class MenuServiceImpl implements MenuService {

	/**
	 * RoleDao
	 */
	@Autowired
	private MenuDaoMapper menuDaoMapper;
	/**
	 * get all menu
	 *@return list
	 */
	public List<Menu> getAllMenu() {
		return menuDaoMapper.getAllMenu();
	}
	/**
	 * get some user's menus
	 * @param userId id
	 * @return list
	 */
	public List<Menu> getMenuByUserId(int userId) {
		return menuDaoMapper.getMenuByUserId(userId);
	}
	/**
	 * @param menu menu
	 */
	@Transactional
	public void addMenu(Menu menu) {
		this.menuDaoMapper.addMenu(menu);
	}
	/**
	 * @param idArray idArray
	 */
	@Transactional
	public void deleteMenuByIDs(int[] idArray) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ids", idArray);
		this.menuDaoMapper.deleteMenuByIDs(map);
	}
	@Override
	public List<Menu> exportMenu(Map<String, Object> beanMap) {
		return this.menuDaoMapper.exportMenu(beanMap);
	}
	@Override
	public Menu getMenuById(int id) {
		return this.menuDaoMapper.getMenuById(id);
	}
	@Override
	public int getMenuCount(Map<String, Object> beanMap) {
		return this.menuDaoMapper.getMenuCount(beanMap);
	}
	@Override
	public int getMenuTreeCount(Map<String, Object> beanMap) {
		return this.menuDaoMapper.getMenuTreeCount(beanMap);
	}
	@Override
	public List<Menu> queryMenu(Map<String, Object> beanMap) {
		return this.menuDaoMapper.queryMenu(beanMap);
	}
	@Override
	public List<Menu> queryMenuTree(Map<String, Object> beanMap) {
		return this.menuDaoMapper.queryMenuTree(beanMap);
	}
	/**
	 * @param menu menu
	 */
	@Transactional
	public void updateMenu(Menu menu) {
		this.menuDaoMapper.updateMenu(menu);
	}
	/**
	 * @return list
	 */
	public List<Menu> getAllParentMenus() {

		return this.menuDaoMapper.getAllParentMenus();

	}
	@Override
	public List<Menu> getMenuByRoleId(int id) {
		return this.menuDaoMapper.getMenuByRoleId(id);
	}
	/**
	 * @param menuIds ids
	 * @param id roleid
	 */
	@Transactional
	public void updateMenuByRoleId(int id, int[] menuIds) {
		this.menuDaoMapper.removeMenuRealById(id);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menuIds", menuIds);
		map.put("id", id);

		this.menuDaoMapper.addMenuReal(map);
	}
	/**
	 * @param parentId id
	 * @return list
	 */
	public List<Menu> getMenusByParentId(int parentId) {
		return this.menuDaoMapper.getMenusByParentId(parentId);
	}
	@Override
	public String getNoEntranceString(int userId) {
		List<Menu> menus = this.menuDaoMapper.getNoMenuByUserId(userId);
		StringBuffer sb = new StringBuffer("");
		if (null != menus && menus.size() > 0) {
			for (Menu menu : menus) {
				if (null != menu.getUrl()) {
					sb.append(menu.getUrl() + ",");
				}
			}
		}
		return sb.toString();
	}
	/**
	 * @param meId id
	 * @param otherId id
	 */
	@Transactional
	public void changeMenuOrderNum(int meId, int otherId) {
		Menu meMenu = this.menuDaoMapper.getMenuById(meId);
		Menu otherMenu = this.menuDaoMapper.getMenuById(otherId);

		int temp = meMenu.getOrderNum();
		meMenu.setOrderNum(otherMenu.getOrderNum());
		otherMenu.setOrderNum(temp);

		this.menuDaoMapper.updateMenu(meMenu);
		this.menuDaoMapper.updateMenu(otherMenu);
	}
	/**
	 * @param meId id
	 * @param option up down
	 * @throws Exception exception
	 */
	@Transactional
	@SuppressWarnings(value = { "unchecked" })
	public void changeMenuOrderNum(int meId, String option) throws Exception {
		Menu meMenu = this.menuDaoMapper.getMenuById(meId);
		Map<String, Object> map = BeanUtils.describe(meMenu);
		map.put("option", option);
		List<Menu> menus = this.menuDaoMapper.getLevelNab(map);
		Menu nabMenu = null;
		if ("up".equals(option) && menus.size() >= 1) {
			nabMenu = menus.get(menus.size() - 1);
		}
		if ("down".equals(option) && menus.size() >= 1) {
			nabMenu = menus.get(0);
		}
		if (null == nabMenu) {
			return;
		}

		int temp = meMenu.getOrderNum();
		meMenu.setOrderNum(nabMenu.getOrderNum());
		nabMenu.setOrderNum(temp);

		this.menuDaoMapper.updateMenu(meMenu);
		this.menuDaoMapper.updateMenu(nabMenu);

	}


}
